var app = angular.module('myApp', ['myModule']);
app.controller('userCtrl', ['$scope', 'UserService', function($scope, UserService){
	$scope.user={id:null,userName:'',passWord:'',age:''};
	$scope.users=[];
	$scope.curr=1;//当前页
	$scope.all=20;//总页数
	$scope.count=10;//最多显示地页数，默认为10
	$scope.fetchAllUsers = function() {
		UserService.fetchAllUsers($scope.curr).then(function(d) {
			$scope.users = d.data.data;
//			$scope.all = d.data.totle;
		}, function(errResponse) {
			console.error('Error while fetching Currencies');
		});
	};
	$scope.edit = function(id){
		 for(var i = 0; i < $scope.users.length; i++){
             if($scope.users[i].id == id) {
                $scope.user = angular.copy($scope.users[i]);
                break;
             }
		 }
	}
	$scope.delete = function(id){
		UserService.deleteUser(id).then(function(id){
			$scope.fetchAllUsers();
		})
	}
	$scope.updateUser = function(user,id){
		console.log('User updated with id ', id);
		UserService.updateUser(user, id)
        .then(
        		$scope.fetchAllUsers(),
	              function(errResponse){
		               console.error('Error while updating User.');
	              }	
        );
	}
	$scope.create = function(){
		UserService.createUser($scope.user)
		.then(
				$scope.fetchAllUsers,
				function(errResponse){
		               console.error('Error while creating User.');
	              });
		$scope.reset();
	}
	$scope.close = function(){
		$scope.reset();
	}
	$scope.submit = function(){
		if($scope.user.id!=null){
			$scope.updateUser($scope.user, $scope.user.id);
            console.log('User updated with id ', $scope.user.id);
        }
        $scope.reset();
	}
	$scope.reset = function(){
		$scope.user={id:null,userName:'',passWord:'',age:''};
	}
	$scope.fetchAllUsers();
}]);
app.service('UserService',['$http', function($http){
	return {
		fetchAllUsers: function(page) {
			return $http.get('user/?page='+page).then(
					function(response){
						return response.data;
					}, 
					function(errResponse){
						console.error('Error while fetching users');
					}
			);
		},
	createUser: function(user){
		return $http.post('user/', user).then(
				function(response){
					return response.data;
				}, 
				function(errResponse){
					console.error('Error while creating user');
				}
		);
	},
	    
	    updateUser: function(user, id){
	    	console.log('Service User updated with id ', user.age);
				return $http.put('user/'+id, user)
						.then(
								function(response){
									return response.data;
								}, 
								function(errResponse){
									console.error('Error while updating user');
								}
						);
		},
	    
		deleteUser: function(id){
				return $http.delete('user/'+id)
						.then(
								function(response){
									return response.data;
								}, 
								function(errResponse){
									console.error('Error while deleting user');
								}
						);
		}
	}
	
}]);